package com.back.demo.controller;

import com.back.demo.common.CommonResult;
import com.back.demo.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "管理员功能")
@RestController
@RequestMapping("/back/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("导入学生名单")
    @PostMapping("/import-student-info")
    public CommonResult<?> importStudent(@RequestParam("file") MultipartFile file) throws IOException {
        //System.out.println(System.getProperty("user.dir"));
        String information;
        information = adminService.upload(file);
        if (information.equals("文件上传成功")) {
            information = adminService.importStudentInfo(file.getOriginalFilename());
            return CommonResult.success(information);
        }
        return CommonResult.error(50001, information);
    }

    @ApiOperation("导入学生成绩")
    @PostMapping("/import-student-score")
    public CommonResult<?> importStudentScore(@RequestParam("file") MultipartFile file) throws IOException {
        //System.out.println(System.getProperty("user.dir"));
        String information;
        information = adminService.upload(file);
        if (information.equals("文件上传成功")) {
            information = adminService.importStudentScore(file.getOriginalFilename());
            return CommonResult.success(information);
        }
        return CommonResult.error(50001, information);
    }

    @ApiOperation("导出学生成绩")
    @GetMapping("/export-student-score")
    public CommonResult<?> exportStudent(@RequestParam("filename") String filename) throws IOException {
        //System.out.println(System.getProperty("user.dir"));
        adminService.exportScore(filename);
        return CommonResult.success("导出成功");
    }

    @ApiOperation("下载学生成绩")
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam("filename") String filename) throws IOException {

        adminService.download(response, filename);
    }
}
